import {
    separator
} from '@/filters/vueNumeralFilter/filters';

import {
    queryPageListAPI,
    roiDetailBySourceAPI,
    queryAllRoiStatisAPI,
} from '@/api/roi';


export default {
    data() {
        return {
            loading: false, // 加载动画
            tableHeight: document.documentElement.clientHeight - 300, // 表的高度
            list: [],
            fieldList: [],
            currentPage: 1,
            pageSize: 15,
            pageSizes: [15, 30, 45, 60],
            totalRow: 0,
            moneyPageData: {}, // 列表金额
            sortData: {}, // 字段排序
            /** 控制详情展示 */
            rowID: '', // 行信息
            rowType: '', // 详情类型
            showDview: false,
            name: '',
            deptIds: [],
            startDate: null,
            endDate: null,
            extraData: {},

        }
    },
    computed: {
        getMoney() {
            return separator(this.moneyPageData.putinMoneySum || 0);
        },
    },
    mounted() {
        this.getFieldList()
    },
    methods: {
        /** 获取异步数据 */
        getList() {
            let params = {};
            this.loading = true;
            /** 排序操作 */
            if (this.sortData.order) {
                params.sortField = this.sortData.prop
                params.order = this.sortData.order == 'ascending' ? 2 : 1
            }

            if (this.crmType === 'consume') {
                params = {
                    ...params,
                    putinPlatform: this.name,
                    startPutinFormDate: this.startDate,
                    endPutinFormDate: this.endDate,
                    deptIds: this.deptIds,
                    page: this.currentPage,
                    limit: this.pageSize,
                    isAuth: 0
                };
                queryPageListAPI(params).then(res => {
                    this.loading = false;
                    this.list = res.data.list;
                    this.totalRow = res.data.totalRow;
                    this.extraData = res.data.extraData; // 消耗金额总计
                })
                    .catch(() => {

                    })
            } else if (this.crmType === 'consumeDept') {
                params = {
                    ...params,
                    putinPlatform: this.name,
                    startPutinFormDate: this.startDate,
                    endPutinFormDate: this.endDate,
                    deptIds: this.deptIds,
                    page: this.currentPage,
                    limit: this.pageSize,
                    isAuth: 1
                };
                queryPageListAPI(params).then(res => {
                    this.loading = false;
                    this.list = res.data.list;
                    this.totalRow = res.data.totalRow;
                    this.extraData = res.data.extraData; // 消耗金额总计
                })
                    .catch(() => {

                    })
            } else if (this.crmType === 'channel') {
                params = {
                    ...params,
                    putinPlatform: this.name,
                    startPutinFormDate: this.startDate,
                    endPutinFormDate: this.endDate,
                    deptIds: this.deptIds,
                    page: this.currentPage,
                    limit: this.pageSize,
                }
                roiDetailBySourceAPI(params).then(res => {
                    this.loading = false;
                    this.list = res.data.list;
                    this.totalRow = res.data.totalRow;
                    this.extraData = res.data.extraData;
                })
                    .catch(() => {

                    })

            } else if (this.crmType === 'groupAllStatis') { //集团总统计
                params = {
                    ...params,
                    putinPlatform: this.name,
                    page: this.currentPage,
                    limit: this.pageSize,
                }
                queryAllRoiStatisAPI(params).then(res => {
                    this.loading = false;
                    this.list = res.data.list;
                    this.totalRow = res.data.totalRow;
                    this.extraData = res.extraData; // 消耗金额总计
                })
                    .catch(() => { })
            }
        },

        /** 获取字段 */
        getFieldList() {
            let data = [];
            if (this.crmType === 'consume' || this.crmType === 'consumeDept') {
                data = [
                    {
                        fieldName: 'putinPlatform',
                        name: '渠道名称',
                        id: 1,
                        sort: true,
                        prop: 'putinPlatform'
                    },
                    {
                        fieldName: 'putinMoney',
                        name: '消耗金额',
                        id: 13,
                        sort: true,
                        prop: 'putinMoney'
                    },
                    {
                        fieldName: 'putinFormDate',
                        name: '消耗日期',
                        id: 3,
                        sort: true,
                        prop: 'updateTime'
                    },
                    {
                        fieldName: 'deptName',
                        name: '消耗所属部门',
                        id: 9,
                        sort: true,
                        prop: 'deptName'
                    },
                    {
                        fieldName: 'createTime',
                        name: '创建时间',
                        id: 2,
                        sort: true,
                        prop: 'createTime'
                    },
                    {
                        fieldName: 'createUserName',
                        name: '创建人',
                        id: 3,
                        sort: true,
                        prop: 'createUserName'
                    },
                ]
            } else if (this.crmType === 'channel') { // ROI明细
                data = [
                    {
                        fieldName: 'putinPlatform',
                        name: '渠道名称',
                        id: 1,
                        sort: false,
                        prop: 'putinPlatform'
                    },
                    {
                        fieldName: 'putinFormDate',
                        name: '消耗日期',
                        id: 2,
                        sort: false,
                        prop: 'putinFormDate'
                    },
                    {   // 线索数量
                        fieldName: 'leadsCount',
                        name: '线索数量',
                        id: 3,
                        sort: false,
                        prop: 'leadsCount'
                    },
                    {
                        fieldName: 'putinMoney',
                        name: '消耗金额',
                        id: 4,
                        sort: false,
                        prop: 'putinMoney'
                    },
                    {
                        fieldName: 'deptName',
                        name: '消耗部门',
                        id: 5,
                        sort: false,
                        prop: 'deptName'
                    },
                    {
                        fieldName: 'receivablesMoney',
                        name: '回款金额',
                        id: 6,
                        sort: false,
                        prop: 'receivablesMoney'
                    },
                    {
                        fieldName: 'refundMoney',
                        name: '退款金额',
                        id: 7,
                        sort: true,
                        prop: 'refundMoney'
                    },
                    {
                        fieldName: 'expectReceivablesMoney',
                        name: '预期回款金额',
                        id: 8,
                        sort: true,
                        prop: 'expectReceivablesMoney'
                    }
                ]
            } else if (this.crmType === 'groupAllStatis') {
                data = [
                    // 渠道名称 消耗日期 线索数量 消耗金额 消耗部门 回款金额 退款金额 预收益回款金额
                    {
                        fieldName: 'putinPlatform',
                        name: '渠道名称',
                        id: 1,
                        sort: false,
                        prop: 'putinPlatform'
                    },
                    {
                        fieldName: 'averageRoi',
                        name: '平均ROI',
                        id: 2,
                        sort: false,
                        prop: 'averageRoi'
                    },
                    {
                        fieldName: 'realityRoi',
                        name: '实际ROI',
                        id: 3,
                        sort: false,
                        prop: 'realityRoi'
                    },
                    {
                        fieldName: 'expectRoi',
                        name: '预期ROI',
                        id: 4,
                        sort: false,
                        prop: 'expectRoi'
                    },
                    {
                        fieldName: 'averagePrice',
                        name: '平均客单价',
                        id: 5,
                        sort: false,
                        prop: 'averagePrice'
                    },
                    {
                        fieldName: 'realityPrice',
                        name: '实际客单价',
                        id: 7,
                        sort: false,
                        prop: 'realityPrice'
                    },
                ]
            }
            const fieldList = []
            const moneyFields = []
            for (let index = 0; index < data.length; index++) {
                const element = data[index]

                // 金额字段 需要格式化
                if (element.formType === 'floatnumber') {
                    moneyFields.push(element.fieldName || '')
                }

                fieldList.push({
                    prop: element.fieldName,
                    label: element.name,
                    sortId: element.id
                })
            }

            this.moneyFields = moneyFields
            this.fieldList = fieldList
            // 获取好字段开始请求数据
            this.getList()
        },
        /** 搜索 */
        crmSearch(val) {
            this.currentPage = 1
            this.deptIds = val.deptId;
            this.name = val.name;
            this.getList();
        },
        // 渠道名称
        nameChange(val) {
            this.name = "";
            this.name = val.name;
        },
        dateChange(val) {
            if (val.date && val.date.length > 0) {
                this.startDate = val.date[0] + ' 00:00:00';
                this.endDate = val.date[1] + ' 23:59:59';
            } else {
                this.startDate = null;
                this.endDate = null;
            }

        },
        /** 格式化字段 */
        fieldFormatter(row, column, cellValue) {
            if (this.moneyFields.includes(column.property)) {
                return separator(row[column.property] || 0)
            }
            return row[column.property] === '' || row[column.property] === null ? '--' : row[column.property]
        },

        /** 字段排序 */
        sortChange(column, prop, order) {
            this.currentPage = 1
            this.sortData = column || {}
            this.getList()
        },

        // 更改每页展示数量
        handleSizeChange(val) {
            this.pageSize = val;
            this.getList()
        },
        // 更改当前页数
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getList()
        },
    }
}
